package com.yunyao.dao.service.bamu.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunyao.common.constant.enums.DataDeletedStatus;
import com.yunyao.dao.model.*;
import com.yunyao.dao.mapper.BamuPoSupplyMapper;
import com.yunyao.dao.service.bamu.IBamuPoSupplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunyao.dao.service.bamu.IBamuReconciliationService;
import com.yunyao.dao.service.bamu.IBamuSpReconciliationService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 销售合同订单供货 服务实现类
 * </p>
 *
 * @author xingjishuai
 * @since 2024-07-02
 */
@Service
public class BamuPoSupplyServiceImpl extends ServiceImpl<BamuPoSupplyMapper, BamuPoSupply> implements IBamuPoSupplyService {
@Autowired
private IBamuSpReconciliationService iBamuSpReconciliationService;
    @Override
    public List<BamuPoSupply> listByIdsDeleted(List<String> MaterialIdList) {
        // 检查对账单ID列表是否为空
        if (CollectionUtils.isEmpty(MaterialIdList)) {
            return List.of(); // 如果为空，则直接返回空列表
        }
        // 创建查询条件封装器
        LambdaQueryWrapper<BamuPoSupply> wrapper =
                new LambdaQueryWrapper<BamuPoSupply>().in(BamuPoSupply::getId,
                        MaterialIdList).eq(BamuPoSupply::getIsDeleted,
                        DataDeletedStatus.NO.getType());
        // 使用封装器查询对账单列表
        return list(wrapper);
    }

    @Override
    public List<BamuPoSupply> listByReconciliationSettlementIdDeleted(String reconciliationId, String ContractId) {
        // 检查对账ID列表是否为空，如果为空则直接返回空列表
        if (org.apache.commons.lang3.StringUtils.isBlank(ContractId)) {
            return List.of();
        }
        LambdaQueryWrapper<BamuSpReconciliation> wrapper1 =
                new LambdaQueryWrapper<BamuSpReconciliation>()
                        .eq(BamuSpReconciliation::getContractId, ContractId)
                        .eq(BamuSpReconciliation::getId, reconciliationId);
        List<BamuSpReconciliation> bamuSpReconciliationList = iBamuSpReconciliationService.list(wrapper1);
        // 从BamuSpReconciliation列表中提取ContractId
        List<String> contractIdList = bamuSpReconciliationList.stream()
                .map(BamuSpReconciliation::getContractId)
                .collect(Collectors.toList());
        // 创建查询条件封装Lambda表达式，用于筛选特定条件的对账结算单
        LambdaQueryWrapper<BamuPoSupply> wrapper =
                new LambdaQueryWrapper<BamuPoSupply>()
                        // 条件1：对账ID在给定的列表中
                        .in(BamuPoSupply::getContractId,
                                contractIdList)
                        // 条件2：对账结算单的删除状态为未删除
                        .eq(BamuPoSupply::getIsDeleted,
                                DataDeletedStatus.NO.getType());
        return list(wrapper);
    }

    @Override
    public List<BamuPoSupply> listById(String Id) {
        // 检查合同ID是否为空或空白，如果是，则返回一个空列表
        if (StringUtils.isBlank(Id)) {
            return List.of();
        }
        // 创建一个LambdaQueryWrapper用于构建查询条件
        LambdaQueryWrapper<BamuPoSupply> wrapper =
                new LambdaQueryWrapper<BamuPoSupply>().eq(BamuPoSupply::getId,
                        Id).eq(BamuPoSupply::getIsDeleted, DataDeletedStatus.NO.getType());
        // 使用构建的查询条件获取并返回关联的材料列表
        return list(wrapper);
    }
}
